# Austin Laugesen
# This python script demonstrates how to connect to a website, login and retrieve HTML.
# We can parse this HTML for our component testing.

# Necessary libraries for pulling info from the Internet
import cookielib, urllib2, re

# Get a handle on the session information stored in cookies.
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# This is the URL from which we want to retrieve HTML.
login_url = 'http://www.alaug.com/website/authentication/index.php'
#login_url = 'http://localhost/~Austin/macrohard/website/authentication/index.php'

# Information we want to pass in a POST so we successfully login.
# this gets appended to the end of a URL, for example:
# http://www.alaug.com/website/authentication/index.php?email=john@live.com&password=john&submit=submit
# http://localhost/~Austin/macrohard/website/authentication/index.php?email=john@live.com&password=john&submit=submit

''' ######## INPUTS - for login ########'''
login_form = 'email=john@live.com&password=john&submit=submit'

# Below is where the login occurs! We're passing the proper POST
# to the authentication page, retrieving HTML and storing it in the variable 'page'
page = opener.open(login_url, login_form)

# print the contents of the page we connected to in the console
htmlPulledFromServer = page.read()

output = 'John'
m = re.search(output, htmlPulledFromServer)

### As a tester we only care about when m == None, this means the test failed! :(
if None == m:
	print 'didn\'t find ' + output
else:
	print 'found ' + output

# close our connection with the current page
page.close()

# Let's go to another page and verify we're still logged in...
#    we'd have to look for 'Logout' in the navbar
#    I didn't write code to do this, but you can make this observation in the HTML
#    placed in the terminal
#edit_user_url = 'http://localhost/~Austin/macrohard/website/users/edit.php'
edit_user_url = 'http://www.alaug.com/website/users/edit.php'
#http://www.alaug.com/website/authentication/index.php

''' ######## INPUTS - for editing user info ########'''
edit_form = 'id=1&salt=11672993034ec49a26d17f2&firstname=John&lastname=Doe&email=john@live.com&address=111+Bowery&city=Iowa+City&state=IA&dob=1989-02-24&ssn=3-32-321&passwordhash=&question=What+town+were+you+born+in?&answerhash=&submit=submit'
page = opener.open(edit_user_url, edit_form)

# we are now redirected the user homepage...
page = opener.open(edit_user_url)

# Notice that since we still have a handle on our session information
# we are still logged in when we go to another page
htmlPulledFromServer = page.read()

'''######## OUTPUTS ########'''
output = 'Doe'

####### SEARCH FOR OUTPUTS
# Go through HTML to verify the right information is displayed.

m = re.search(output, htmlPulledFromServer)
### As a tester we only care about when m == None, this means the test failed! :(
if None == m:
	print 'didn\'t find ' + output
else:
	print 'found ' + output

page.close()

##### We'll change the user's info back to it's original value
edit_form = 'id=1&salt=11672993034ec49a26d17f2&firstname=John&lastname=Doh&email=john@live.com&address=111+Bowery&city=Iowa+City&state=IA&dob=1989-02-24&ssn=3-32-321&passwordhash=&question=What+town+were+you+born+in?&answerhash=&submit=submit'
page = opener.open(edit_user_url, edit_form)

page.close()
